import { createRouter, createWebHistory } from 'vue-router';
import { routers } from './options';
import user from "../user";
import store from "../store";

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes: routers
});

router.beforeEach(function (to: any, from: any, next: Function) {
  // 是否登录
  if (to.name !== 'login' && !user.isAuthentication()) {
    next('/login');
    return;
  }

  // 是否有权限
  if (to.name !== 'login' && !to.meta.allow) {
    next('/401');
    return;
  }
  next();
})

export default router;